<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .contentsBox {
            width: 200px;
            height: 200px;
            background-color: #87ceeb;
            display: none;
        }

        .active {
            display: block;
        }
    </style>
</head>

<body>
    <div id="app">
        <!-- 标题 -->
        <com_title @comtitleevent="fatherCallback"></com_title>

        <!-- 内容 -->
        <com_content :props_index="changeIndex"></com_content>
    </div>


    <script src="../vue.js"></script>


    <script>
        // 标题组件
        let ComTitle = {
            data() {
                return {
                    titleArr: [{
                            id: 1,
                            title: "html"
                        },
                        {
                            id: 2,
                            title: "css"
                        },
                        {
                            id: 3,
                            title: "js"
                        },
                        {
                            id: 4,
                            title: "vuejs"
                        }
                    ]
                }
            },
            template: `
            <div>
                <button 
                    v-for="(item, index) in titleArr"
                    @click="comTitleFn(index)"
                >{{item.title}}</button>
            </div>
            `,
            methods: {
                comTitleFn(index) {
                    this.$emit("comtitleevent", index);
                }
            }
        }

        // 内容组件
        let ComContent = {
            props: {
                props_index: Number
            },
            data() {
                return {
                    contentsArr: [{
                            id: 1,
                            contents: "html-contents"
                        },
                        {
                            id: 2,
                            contents: "css-contents"
                        },
                        {
                            id: 3,
                            contents: "js-contents"
                        },
                        {
                            id: 4,
                            contents: "vuejs-contents"
                        }
                    ]
                }
            },
            template: `
            <div>
                <div 
                    v-for="(item, index) in contentsArr" 
                    :class="{contentsBox:true, active:(index == props_index)}"
                >{{item.contents}}</div>
            </div>
            `
        }


        new Vue({
            el: "#app",
            data: {
                changeIndex: 0
            },
            components: {
                com_title: ComTitle,
                com_content: ComContent
            },
            methods: {
                fatherCallback(res) {
                    this.changeIndex = res;
                }
            }
        })
    </script>
</body>

</html>